package org.popupcommunication.dao.impl;

import java.util.List;

import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.popupcommunication.dao.GroupDAO;
import org.popupcommunication.dao.UserDAO;
import org.popupcommunication.exceptions.UserNotFoundException;
import org.popupcommunication.model.Agenda;
import org.popupcommunication.model.AgendaGroupe;
import org.popupcommunication.model.Group;
import org.popupcommunication.model.Message;
import org.popupcommunication.model.User;
import org.springframework.beans.factory.annotation.Autowired;

//@Repository  
public class UserJpa extends GenericRepositoryJpa<User> implements UserDAO {
	@Autowired
	GroupDAO groupRepository;

	@Override
	public User findByEmail(String email) {
		TypedQuery<User> query = this.getEntityManager().createQuery(
				"SELECT u FROM User u", User.class);
		List<User> results = query.getResultList();
		User toto = null;
		for (User user : results) {
			if (user.getEmail().equals(email)) {
				toto = user;
			}
		}
		// this.getEntityManager().close();
		return toto;
	}

	@Override
	public int getIdUser(String email) throws UserNotFoundException {
		User toto = null;
		toto = findByEmail(email);
		if (toto == null) {
			throw new UserNotFoundException(email);
		}
		return toto.getIdUser();
	}

	@Override
	public void updateUser(User toto) {
		User toUpdate = this.findById(toto.getIdUser());
		toUpdate.setFirstName(toto.getFirstName());
		toUpdate.setLastName(toto.getLastName());
		toUpdate.setEmail(toto.getEmail());
		toUpdate.setFonction(toto.getFonction());
		toUpdate.setGsm(toto.getGsm());
		toUpdate.setTel(toto.getTel());
		toUpdate.setInstitution(toto.getInstitution());
		toUpdate.setActivate(toto.isActivate());
		toUpdate.setAdmin(toto.isAdmin());
		toUpdate.getListGroup().clear();
		if (toto.getListGroup() != null) {
			for (Group g : toto.getListGroup()) {
				g = groupRepository.merge(g);
				toUpdate.addGroup(g);
				g.addUser(toUpdate);
			}
		}
		this.save(toUpdate);
	}

	@Override
	public List<User> getUsers() {
		TypedQuery<User> query = this.getEntityManager().createQuery(
				"SELECT u FROM User u", User.class);
		List<User> results = query.getResultList();
		return results;
	}

	@Override
	public List<User> getUsersWaitingValidation() {
		Query query = this
				.getEntityManager()
				.createQuery(
						"SELECT u FROM User u WHERE u.isActivate=:statut AND u.dateReactivate=null");
		query.setParameter("statut", false);
		List<User> lst = (List<User>) query.getResultList();
		return lst;
	}

	@Override
	public List<User> getAdmins() {
		Query query = this
				.getEntityManager()
				.createQuery(
						"SELECT u FROM User u WHERE u.isAdmin = true");
		List<User> lst = (List<User>) query.getResultList();
		return lst;
	}

	@Override
	public List<Agenda> getCurrentsEventsUsers(User u) {
		Query query = this
				.getEntityManager()
				.createNativeQuery(
						"SELECT a.* FROM agenda a"
						+ " left join groupes g on a.idGroup = g.idGroup  "
						+ " WHERE a.idGroup in (select distinct listGroup_idgroup from users_groupes where listUser_idUser = :id)"
						+ " and  a.dateExpiration > now()"
						+ " and a.idUser is null"						
						+ " order by a.dateExpiration" , Agenda.class);
		query.setParameter("id", u.getIdUser());
		List<Agenda> lst = (List<Agenda>) query.getResultList();
		return lst;
	}

	@Override
	public List<Agenda> getCurrentsEventsPersoUsers(User u) {
		Query query = this
				.getEntityManager()
				.createNativeQuery(
						"SELECT a.* FROM agenda a"
						+ " WHERE a.dateExpiration > now()"
						+ " and a.idUser is not null"
						+ " order by a.dateExpiration" , Agenda.class);
		List<Agenda> lst = (List<Agenda>) query.getResultList();
		return lst;
	}

}
